package com.dunshan.prcexp.request.sender;

import com.dunshan.prcexp.config.Config;
import com.dunshan.prcexp.pojo.HttpEntity;
import com.dunshan.prcexp.response.content.ResultContent;
import okhttp3.Headers;
import okhttp3.Response;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @author liwen406
 * @program: dunshan-pef
 * @description:
 * @date 2022-08-24 20:50:16
 */
public class HttpRequestSender {
    private HttpEntity httpEntity;

    public HashMap<String, String> getResponseHeaders(Response response) {
        HashMap<String, String> result = new HashMap();
        if (response.priorResponse() != null) {
            result.putAll(this.getResponseHeaders(response.priorResponse()));
        }

        if (response.networkResponse() != null) {
            result.putAll(this.getResponseHeaders(response.networkResponse()));
        }

        Headers headers = response.headers();
        String cookie = "";
        Iterator var5 = headers.names().iterator();
        while (true) {
            while (var5.hasNext()) {
                String name = (String) var5.next();
                if (!"Set-Cookie".equalsIgnoreCase(name) && !"Cookie".equalsIgnoreCase(name)) {
                    result.put(name, headers.get(name));
                } else {
                    List<String> values = headers.values(name);
                    Iterator var8 = values.iterator();

                    while (var8.hasNext()) {
                        String cookieVal = (String) var8.next();
                        cookie = cookie + ";" + cookieVal;
                        String[] vals = cookie.split(";");
                        String[] var11 = vals;
                        int var12 = vals.length;

                        for (int var13 = 0; var13 < var12; ++var13) {
                            String val = var11[var13];
                            int index = val.indexOf("=");
                            if (index > 0) {
                                result.put(val.substring(0, index), val.substring(index + 1, val.length()));
                            }
                        }
                    }
                }
            }

            if (cookie.length() > 0) {
                result.put("Cookie", cookie.substring(1));
            }

            result.put("code", String.valueOf(response.code()));
            return result;
        }
    }

    public ResultContent executePost(Config config, HttpEntity httpEntity, String description) {
        return null;
    }
}
